<?php

class TagTable extends Doctrine_Table {

    public static function getInstance() {
        return Doctrine_Core::getTable('Tag');
    }

    /**
     * This method deletes tags associated with the specified Link and returns
     * the number of deleted elements.
     * @param integer $linkId
     * @return integer
     */
    public function deleteTagsForLink($linkId) {
        $q = $this->createQuery('t')
                        ->delete()
                        ->addWhere('t.link_id = ?', $linkId);
        return $q->execute();
    }

    /**
     * Returns an array with the different Tags in the DB in the form
     * array (
     *      'tagname' => 'number_of_links',
     * )
     * @return array
     */
    public function getTagsArray() {
        $q = $this->createQuery('t');
        $res = $q->execute();
        $tags = array();
        foreach ($res as $row) {
            if (array_key_exists($row['keyword'], $tags)) {
                $tags[$row['keyword']] += 1;
            } else {
                $tags[$row['keyword']] = 1;
            }
        }
        return $tags;
    }

}